package com.lily.test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author lily
 */
public class testRedis {

    /**
     * key value
     * key：String
     * value：
     *      1.String
     *      2.hash
     *      3.list
     *      4.set
     *      5.zset
     */
    private Jedis jedis=null;

    @Before
    public void init(){
        //1.获取Jedis对象
        jedis = new Jedis();
    }

    @Test
    public void testString(){
        jedis.set("name","lily");
        String name = jedis.get("name");
        System.out.println(name);
    }

    @Test
    public void testHash(){
        jedis.hset("myhash", "k1", "v1");
        jedis.hset("myhash", "k2", "v2");
        jedis.hset("myhash", "k3", "v3");

        String v1 = jedis.hget("myhash", "k1");
        System.out.println(v1);

        Map<String, String> map = jedis.hgetAll("myhash");
        for (String str: map.keySet()) {
            System.out.println(str);
        }
    }

    /**
     * list：
     *  底层：双向链表结构
     *  特点：有序（按照添加的顺序）+ 可重复
     */
    @Test
    public void testList(){
        jedis.lpush("myList","list1","list2","list3");
        jedis.rpush("myList","list4","list5","list1");

        List<String> myList = jedis.lrange("myList", 0, -1);
        System.out.println(myList);
    }

    @Test
    public void testSet(){
        jedis.sadd("mySet","set1","set2","set3");

        Set<String> mySet = jedis.smembers("mySet");
        System.out.println(mySet);
    }

    @Test
    public void testzset(){
        jedis.zadd("myzset",50.0,"sgz");
        jedis.zadd("myzset",100.0,"lbc");
        jedis.zadd("myzset",200.0,"hk");

        Set<String> myzset = jedis.zrange("myzset", 0, -1);
        System.out.println(myzset);
    }

    @After
    public void destroy(){
        //释放资源
        jedis.close();
    }
}
